local s_uiinfo = s_uiinfo or {}

local tmp_uiinfo = UIInfo.create()
------------------------------------------------------------
ResultPanel = class("ResultPanel")

function ResultPanel.create(view)
    local panel = ResultPanel.new()
    panel:initWithView(view)

    return panel
end
 
function ResultPanel:initWithView(view)
    self.view = view

    do
        local fnt_title = uiUtil.createBMFontByHolder(self.view, "_title", pp.fnt_level_ret)
        fnt_title:setString(string.format("关卡 %d", gameData.level))
        fnt_title:setScale(1.2)
    end
 
    do
        self.star1 = self.view:getChildByName("_star1")
        self.star2 = self.view:getChildByName("_star2")
        self.star3 = self.view:getChildByName("_star3")
        self.star1:setVisible(false)
        self.star2:setVisible(false)
        self.star3:setVisible(false)

    end

    --bts
    do
        local function btAgainCallback(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
             elseif eventType == ccui.TouchEventType.ended then
                 self:playAgain()
                 self.bt_again:setEnabled(false)
             end
        end
        self.bt_again = self.view:getChildByName("_again_bt")
        self.bt_again:addTouchEventListener(btAgainCallback)
        
        
        --next
        local function btNextCallback(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
             elseif eventType == ccui.TouchEventType.ended then
                 self:playNext()
                self.bt_next:setEnabled(false)
             end
        end
        self.bt_next = self.view:getChildByName("_next_bt")
        self.bt_next:addTouchEventListener(btNextCallback)
        
        --qianghua
        local function btQianghuaCallback(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
             elseif eventType == ccui.TouchEventType.ended then
                 self:qianghua()
                self.bt_qianghua:setEnabled(false)
             end
        end
        self.bt_qianghua = self.view:getChildByName("_qianghua_bt")
        self.bt_qianghua:addTouchEventListener(btQianghuaCallback)
        
        --
        -- local function btCloseCallback(sender, eventType)
        --      if eventType == ccui.TouchEventType.ended then
        --          self:close()
        --      end
        -- end
        -- local bt_close = self.view:getChildByName("_close")
        -- bt_close:addTouchEventListener(btCloseCallback)
        local function btBackToLevel(sender, eventType)
            if eventType == ccui.TouchEventType.began then
                AudioEngine.playEffect(sound.btn1)
            elseif eventType == ccui.TouchEventType.ended then
                gameUtil.changeScene("level")
                
            end
        end
        local bt_back = self.view:getChildByName("_backToLevel")
        bt_back:addTouchEventListener(btBackToLevel)
    end

    --cur score
    do
        local fnt = self.view:getChildByName("_fnt_cur_score_num")
        local fntstr = string.format("%d", gameData.curScore)
        self.fnt_curScore = cc.LabelBMFont:create(fntstr, pp.fnt_number_yellow)
        self.fnt_curScore:setScale(0.7);
        self.fnt_curScore:setPosition(fnt:getPosition())   
        self.view:addChild(self.fnt_curScore)
        self.fnt_curScore:setLocalZOrder(10)
        fnt:removeFromParent(true)
    end
    
    --former score
    do
        local fnt = self.view:getChildByName("_fnt_former_score_num")
        --local fntstr = string.format("%d", gameData.curScore)
        self.fnt_historyScore = cc.LabelBMFont:create("0", pp.fnt_number_white)
        self.fnt_historyScore:setScale(0.7);
        self.fnt_historyScore:setPosition(fnt:getPosition())   
        self.view:addChild(self.fnt_historyScore)
        self.fnt_historyScore:setLocalZOrder(10)
        fnt:removeFromParent(true)
    end

    do
        self.ui_hightscore_bg = self.view:getChildByName("_hightscore_bg")
    end

    --award icons
    do
        self.icon_award1 = self.view:getChildByName("_award1")
        self.icon_award2 = self.view:getChildByName("_award2")
        self.icon_award3 = self.view:getChildByName("_award3")
        self.icon_award4 = self.view:getChildByName("_award4")
    end

    --award1
    do
        local fnt = self.view:getChildByName("_fnt_award1")
        --local fntstr = string.format("%d", gameData.curScore)
        self.fnt_award1  = cc.LabelBMFont:create("", pp.fnt_number_white)
        self.fnt_award1:setScale(0.5);
        self.fnt_award1:setPosition(fnt:getPosition())   
        self.view:addChild(self.fnt_award1)
        self.fnt_award1:setLocalZOrder(10)
        fnt:removeFromParent(true)
    end

    --award2
    do
        local fnt = self.view:getChildByName("_fnt_award2")
        --local fntstr = string.format("%d", gameData.curScore)
        self.fnt_award2 = cc.LabelBMFont:create("", pp.fnt_number_white)
        self.fnt_award2:setScale(0.5);
        self.fnt_award2:setPosition(fnt:getPosition())   
        self.view:addChild(self.fnt_award2)
        self.fnt_award2:setLocalZOrder(10)
        fnt:removeFromParent(true)
    end

    --award3
    do
        local fnt = self.view:getChildByName("_fnt_award3")
        --local fntstr = string.format("%d", gameData.curScore)
        self.fnt_award3 = cc.LabelBMFont:create("", pp.fnt_number_white)
        self.fnt_award3:setScale(0.5);
        self.fnt_award3:setPosition(fnt:getPosition())   
        self.view:addChild(self.fnt_award3)
        self.fnt_award3:setLocalZOrder(10)
        fnt:removeFromParent(true)
    end

    --award4
    do
        local fnt = self.view:getChildByName("_fnt_award4")
        --local fntstr = string.format("%d", gameData.curScore)
        self.fnt_award4 = cc.LabelBMFont:create("", pp.fnt_number_white)
        self.fnt_award4:setScale(0.5);
        self.fnt_award4:setPosition(fnt:getPosition())   
        self.view:addChild(self.fnt_award4)
        self.fnt_award4:setLocalZOrder(10)
        fnt:removeFromParent(true)
    end
    
    --failwords
    do
        self.fnt_failwords = self.view:getChildByName("_fail_words")
    end
end

function ResultPanel:show()
    self.view:setVisible(true)
    if gameData.curStars == 0 then
        self.star1:setVisible(false)
        self.star2:setVisible(false)
        self.star3:setVisible(false)
    elseif gameData.curStars == 1 then
        self.star1:setVisible(true)
        self.star2:setVisible(false)
        self.star3:setVisible(false)
    elseif gameData.curStars == 2 then
        self.star1:setVisible(true)
        self.star2:setVisible(true)
        self.star3:setVisible(false)
    elseif gameData.curStars == 3 then
        self.star1:setVisible(true)
        self.star2:setVisible(true)
        self.star3:setVisible(true)
    end

    delay = 0.8
    local stars = {self.star1, self.star2, self.star3}

    local function starSound(sender,table)
        AudioEngine.playEffect(sound.starPop[table[1]])
    end

    for k,v in ipairs(stars)do
        if v:isVisible() == true then
            v:setOpacity(0)
            v:setScale(0.5)
            v:runAction(
                cc.Sequence:create(
                    cc.DelayTime:create(delay),
                    cc.Spawn:create(
                            cc.FadeIn:create(0.5),
                            --cc.EaseOut:create(cc.ScaleTo:create(0.4,1), 32)
                        cc.EaseElasticOut:create(cc.ScaleTo:create(1.1,0.8)),
                        cc.CallFunc:create(starSound,{k})
                    )
                )
               )
            delay = delay + 0.4
        end
    end

    local fntstr = string.format("%d", gameData.curScore)
    self.fnt_curScore:setString(fntstr)

    local historystr = string.format("%d", gameData.historyScore)
    self.fnt_historyScore:setString(historystr)

    if gameData.curScore > gameData.historyScore then
        self.ui_hightscore_bg:setVisible(true)
    else
        self.ui_hightscore_bg:setVisible(false)
    end


    if gameData.status == pp.gamestate_fail then
        -- self.bt_next:setEnabled(false)
        -- self.bt_next:loadTextureNormal("common/xiayiguan-h.png", ccui.TextureResType.plistType)
        self.bt_next:setVisible(false)
        self.bt_qianghua:setVisible(true)
        uiEffect.jelly(self.bt_qianghua)
        
        local titlewin = self.view:getChildByName("_title_win")
        titlewin:setVisible(false)

        --
        self.icon_award1:setVisible(false)
        self.icon_award2:setVisible(false)
        self.icon_award3:setVisible(false)
        self.icon_award4:setVisible(false)
        self.fnt_award1:setVisible(false)
        self.fnt_award2:setVisible(false)
        self.fnt_award3:setVisible(false)
        self.fnt_award4:setVisible(false)

        self.fnt_failwords:setVisible(true)

    elseif gameData.status == pp.gamestate_win then
        self.bt_next:setVisible(true)
        self.bt_qianghua:setVisible(false)
        uiEffect.jelly(self.bt_next)

        local titlefail = self.view:getChildByName("_title_fail")
        titlefail:setVisible(false)

        --
        self.icon_award1:setVisible(true)
        self.fnt_award1:setVisible(true)  
        self.fnt_award1:setString("×1")  --forever "1"

        if gameData.award.coin > 0 then
            self.icon_award2:setVisible(true)
            self.fnt_award2:setVisible(true)  
            local fff = string.format("×%d",gameData.award.coin)
            self.fnt_award2:setString(fff) 
        else
            self.icon_award2:setVisible(false)
            self.fnt_award2:setVisible(false)
        end

        if gameData.award.fenji > 0 then
            self.icon_award3:setVisible(true)
            self.fnt_award3:setVisible(true)  
            self.fnt_award3:setString(string.format("×%d",gameData.award.fenji)) 
        else
            self.icon_award3:setVisible(false)
            self.fnt_award3:setVisible(false)
        end

        if gameData.award.yuheng > 0 then
            self.icon_award4:setVisible(true)
            self.fnt_award4:setVisible(true)  
            self.fnt_award4:setString(string.format("×%d",gameData.award.yuheng)) 
        else
            self.icon_award4:setVisible(false)
            self.fnt_award4:setVisible(false)
        end

        self.fnt_failwords:setVisible(false)


    end

    --bts
    if gameData.level == pp.levelMax then
        self.bt_next:setEnabled(false)
        self.bt_next:loadTextureNormal("common/xiayiguan-h.png", ccui.TextureResType.plistType)
    end
end

function ResultPanel:playAgain()
    gameUtil.onClickPlayAgainThisLevel()
end

function ResultPanel:playNext()
    -- gameData:nextLevel()

    gameUtil.onClickPlayNextLevel()
end

function ResultPanel:qianghua()
    gameUtil.onClickQianghuaInGameScene()
end

function ResultPanel:close()
    gameUtil.changeScene("level")
end
